package com.kun.cmd.controller;

import cn.hutool.http.HttpResponse;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.kun.cmd.service.DictService;
import com.kun.commonutil.result.Result;
import com.kun.model.entity.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import java.util.List;

/**
 * @Author zhoukun
 * @Date 2021/4/6 21:41
 */
@Api(value = "数据字典接口")
@RestController
@RequestMapping("admin/cmd/dict")
@Slf4j
public class DictController {

    @Autowired
    DictService dictService;

    /**
     * 查询所有数据
     * @return
     */
    @ApiOperation(value = "查询数据字典所有数据")
    @GetMapping("/findAll")
    public Result<List<Dict>> findAll()
    {
        List<Dict> list = dictService.list();
        return Result.ok(list);
    }


    /**
     * 根据数据父id查询子数据列表
     */

    @ApiOperation(value = "查询数据字典所有数据")
    @GetMapping("/findChildDataById/{id}")
    public Result<List<Dict>> findChildDataById(
            @PathVariable(value = "id") Long id
    )
    {
        List<Dict> list = dictService.findChildDataById(id);
        return Result.ok(list);
    }

    /**
     * 根据数据父dictcode查询子数据列表
     * @param dictCode
     * @return
     */
    @ApiOperation(value = "根据dictCode获取下级节点")
    @GetMapping(value = "/findByDictCode/{dictCode}")
    public Result<List<Dict>> findByDictCode(
            @ApiParam(name = "dictCode", value = "节点编码", required = true)
            @PathVariable String dictCode) {
        List<Dict> list = dictService.findByDictCode(dictCode);
        return Result.ok(list);
    }

    /**
     * Excel导出操作
     */
    @ApiOperation(value = "数据字典导出")
    @GetMapping("/exportDict")
    public Result exportDict(HttpServletResponse response) throws IOException {
        dictService.exportDict(response);
        return Result.ok();
    }
    /**
     * Excel导出操作
     */
    @ApiOperation(value = "数据字典导入")
    @RequestMapping("/importDict")
    public Result importDict(MultipartFile file)  {
        dictService.importDict(file);
        return Result.ok();
    }
//    //根据dictcode和value查询
//    @GetMapping("getName/{dictCode}/{value}")
//    public String getName(@PathVariable String dictCode,
//                          @PathVariable String value) {
//        String dictName = dictService.getDictName(dictCode,value);
//        return dictName;
//    }

    //根据value查询
    @GetMapping("getName/{value}")
    public String getName(@PathVariable String value) {
        String dictName = dictService.getDictName(value);
        return dictName;
    }

}
